<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\AdminUserRepository;
use App\Repositories\AdminRoleRepository;
use App\Models\AdminUser;
use App\Models\AdminRole;
use Illuminate\Validation\Rule;

class AdminController extends Controller
{
    protected $Repo;
    protected $Role;

	public function __construct(AdminUserRepository $repo, AdminRoleRepository $role)
	{
		$this->middleware('admin.auth');
        $this->Repo = $repo;
        $this->Role = $role;
	}

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = $this->Repo->getUsers();
        // dd($users);
        return view('admin.users.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $roles = AdminRole::where('id', '<>', '1')->get();
        return view('admin.users.createUser', compact('roles'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'user_name' => 'required|unique:admin_user',
            'phone' => 'nullable|numeric',
            'email' => 'nullable|email|unique:admin_user',
            'roles' => 'required'
        ]);
        $res = $this->Repo->addUser($request);
        if (is_object($res)) {
            $data = [
                'status' => 0,
                'msg' => '添加用户成功',
                'url' => route('admin.users.index')
            ];
        } else {
            $data = [
                'status' => 2,
                'msg' => '添加用户失败'
            ];
        }
        return json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = $this->Repo->getUser($id);
        $roles = $this->Role->getRolesExceptRoot();
        // dd($user->role_ids);
        return view('admin.users.edit', compact('user', 'roles'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'user_name' => ['required', Rule::unique('admin_user')->ignore($id)],
            'phone' => 'nullable|numeric',
            'email' => ['nullable','email', Rule::unique('admin_user')->ignore($id)],
            'roles' => 'required'
        ]);
        $this->Repo->updateUser($request, $id);

        $data = [
            'status' => 0,
            'msg'    => '用户更新成功',
            'url'    => route('admin.users.index')
        ];

        return json_encode($data);

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->Repo->deleteUser($id);
        $data = [
            'status' => 0,
            'msg'    => '用户删除成功',
            'url'    => route('admin.users.index')
        ];
        return json_encode($data);
    }

    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);

        if ($request->isMethod('post')) {
            $this->Repo->deleteUserLists($ids);
            $data = [
                'status' => 0,
                'msg'    => '用户删除成功',
                'url'    => route('admin.users.index')
            ];
            return json_encode($data);
        }
    }
}
